skidinc.achievements = {};
skidinc.achievements.categories = [];
skidinc.achievements.owned = [];
skidinc.achievements.ach = [];

skidinc.achievements.getOwnedAmount = function() {
    var owned = 0;
    
    for (var i = 0; i < skidinc.achievements.owned.length; i++) {
        if (skidinc.achievements.owned[i])
            owned++;
    };
    
    return owned;
};

skidinc.achievements.getLast = function() {
    var list = [];
    
    for (var e = 0; e < skidinc.achievements.categories.length; e++) {
        var category = skidinc.achievements.categories[e];
        
        for (var i = 0; i < skidinc.achievements.ach.length; i++) {
            var achievement = skidinc.achievements.ach[i],
                owned = skidinc.achievements.owned[i];
            
            if (!owned && achievement.category == category) {
                list.push(achievement);
                i = skidinc.achievements.ach.length;
            };
        };
    };
    
    return list;
};

skidinc.achievements.create = function(name, desc, category, icon, varCheck, typeCheck, amount) {
    this.name = name;
    this.desc = desc;
    this.category = category;
    this.icon = icon;
    this.varCheck = varCheck;
    this.typeCheck = typeCheck;
    this.amount = amount;
};

skidinc.achievements.init = function() {
    skidinc.achievements.ach = [
        new skidinc.achievements.create('新手 I', '执行10个脚本。', 'scriptsExecuted', 'fa-list-alt', 'skidinc.script.totalCompleted', '>=', 10),
        new skidinc.achievements.create('新手 II', '执行50个脚本。', 'scriptsExecuted', 'fa-list-alt', 'skidinc.script.totalCompleted', '>=', 50),
        new skidinc.achievements.create('新手 III', '执行250个脚本。', 'scriptsExecuted', 'fa-list-alt', 'skidinc.script.totalCompleted', '>=', 250),
        new skidinc.achievements.create('新手 IV', '执行1000个脚本。', 'scriptsExecuted', 'fa-list-alt', 'skidinc.script.totalCompleted', '>=', 1000),
        new skidinc.achievements.create('新手 V', '执行2500个脚本。', 'scriptsExecuted', 'fa-list-alt', 'skidinc.script.totalCompleted', '>=', 2500),
        new skidinc.achievements.create('黑客 I', '执行10,000个脚本。', 'scriptsExecuted', 'fa-list-alt', 'skidinc.script.totalCompleted', '>=', 10000),
        new skidinc.achievements.create('黑客 II', '执行75,000个脚本。', 'scriptsExecuted', 'fa-list-alt', 'skidinc.script.totalCompleted', '>=', 75000),
        new skidinc.achievements.create('黑客 III', '执行500,000个脚本。', 'scriptsExecuted', 'fa-list-alt', 'skidinc.script.totalCompleted', '>=', 500000),
        new skidinc.achievements.create('黑客 IV', '执行2,500,000个脚本。', 'scriptsExecuted', 'fa-list-alt', 'skidinc.script.totalCompleted', '>=', 2500000),
        new skidinc.achievements.create('黑客 V', '执行10,000,000个脚本。', 'scriptsExecuted', 'fa-list-alt', 'skidinc.script.totalCompleted', '>=', 10000000),
        
        new skidinc.achievements.create('熟练的 I', '达到等级5。', 'levelReached', 'fa-star', 'skidinc.player.level', '>=', 5),
        new skidinc.achievements.create('熟练的 II', '达到等级10。', 'levelReached', 'fa-star', 'skidinc.player.level', '>=', 10),
        new skidinc.achievements.create('熟练的 III', '达到等级25。', 'levelReached', 'fa-star', 'skidinc.player.level', '>=', 25),
        new skidinc.achievements.create('终极答案', '达到等级42。', 'levelReached', 'fa-star', 'skidinc.player.level', '>=', 42),
        new skidinc.achievements.create('导师 I', '达到等级50。', 'levelReached', 'fa-star', 'skidinc.player.level', '>=', 50),
        new skidinc.achievements.create('导师 II', '达到等级60。', 'levelReached', 'fa-star', 'skidinc.player.level', '>=', 60),
        new skidinc.achievements.create('导师 III', '达到等级70。', 'levelReached', 'fa-star', 'skidinc.player.level', '>=', 70),
        new skidinc.achievements.create('导师 IV', '达到等级80。', 'levelReached', 'fa-star', 'skidinc.player.level', '>=', 80),
        new skidinc.achievements.create('导师 V', '达到等级90。', 'levelReached', 'fa-star', 'skidinc.player.level', '>=', 90),
        new skidinc.achievements.create('最高系统', '达到等级100 (最高等级)。', 'levelReached', 'fa-star', 'skidinc.player.level', '>=', 100),
        
        new skidinc.achievements.create('打工者I', '总共赚取$15,000。', 'totalMoney', 'fa-money', 'skidinc.player.totalMoney', '>=', 15000),
        new skidinc.achievements.create('打工者II', '总共赚取$500,000。', 'totalMoney', 'fa-money', 'skidinc.player.totalMoney', '>=', 500000),
        new skidinc.achievements.create('打工者III', '总共赚取$1,000,000。', 'totalMoney', 'fa-money', 'skidinc.player.totalMoney', '>=', 1000000),
        new skidinc.achievements.create('打工者VI', '总共赚取$50,000,000。', 'totalMoney', 'fa-money', 'skidinc.player.totalMoney', '>=', 50000000),
        new skidinc.achievements.create('打工者V', '总共赚取$750,000,000。', 'totalMoney', 'fa-money', 'skidinc.player.totalMoney', '>=', 750000000),
        new skidinc.achievements.create('银行家I', '总共赚取$10.000b。', 'totalMoney', 'fa-money', 'skidinc.player.totalMoney', '>=', 10000000000),
        new skidinc.achievements.create('银行家II', '总共赚取$100.000b。', 'totalMoney', 'fa-money', 'skidinc.player.totalMoney', '>=', 100000000000),
        new skidinc.achievements.create('银行家III', '总共赚取$1.000t。', 'totalMoney', 'fa-money', 'skidinc.player.totalMoney', '>=', 1000000000000),
        new skidinc.achievements.create('银行家IV', '总共赚取$100.000t。', 'totalMoney', 'fa-money', 'skidinc.player.totalMoney', '>=', 100000000000000),
        new skidinc.achievements.create('银行家V', '总共赚取$10.000q。', 'totalMoney', 'fa-money', 'skidinc.player.totalMoney', '>=', 10000000000000000),
        
        new skidinc.achievements.create('网络管理员I', '将你的网络服务器升为等级1。', 'webLevel', 'fa-server', 'skidinc.server.owned[skidinc.server.servers.indexOf("web")]', '>=', 1),
        new skidinc.achievements.create('网络管理员II', '将你的网络服务器升为等级25。', 'webLevel', 'fa-server', 'skidinc.server.owned[skidinc.server.servers.indexOf("web")]', '>=', 25),
        new skidinc.achievements.create('网络管理员III', '将你的网络服务器升为等级50。', 'webLevel', 'fa-server', 'skidinc.server.owned[skidinc.server.servers.indexOf("web")]', '>=', 50),
        new skidinc.achievements.create('网络管理员IV', '将你的网络服务器升为等级100。', 'webLevel', 'fa-server', 'skidinc.server.owned[skidinc.server.servers.indexOf("web")]', '>=', 100),
        new skidinc.achievements.create('网络管理员V', '将你的网络服务器升为等级200。', 'webLevel', 'fa-server', 'skidinc.server.owned[skidinc.server.servers.indexOf("web")]', '>=', 200),
        
        new skidinc.achievements.create('虚拟化I', '将你的远程服务器升级至等级1。', 'telnetLevel', 'fa-server', 'skidinc.server.owned[skidinc.server.servers.indexOf("telnet")]', '>=', 1),
        new skidinc.achievements.create('虚拟化II', '将你的远程服务器升级至等级25。', 'telnetLevel', 'fa-server', 'skidinc.server.owned[skidinc.server.servers.indexOf("telnet")]', '>=', 25),
        new skidinc.achievements.create('虚拟化III', '将你的远程服务器升级至等级50。', 'telnetLevel', 'fa-server', 'skidinc.server.owned[skidinc.server.servers.indexOf("telnet")]', '>=', 50),
        new skidinc.achievements.create('虚拟化IV', '将你的远程服务器升级至等级75。', 'telnetLevel', 'fa-server', 'skidinc.server.owned[skidinc.server.servers.indexOf("telnet")]', '>=', 75),
        new skidinc.achievements.create('虚拟化V', '将你的远程服务器升级至等级100 (最高等级)。', 'telnetLevel', 'fa-server', 'skidinc.server.owned[skidinc.server.servers.indexOf("telnet")]', '>=', 100),
        
        new skidinc.achievements.create('电力I', '将你的电池升为等级5。', 'batteryLevel', 'fa-battery-full', 'skidinc.battery.level', '>=', 5),
        new skidinc.achievements.create('电力II', '将你的电池升为等级10。', 'batteryLevel', 'fa-battery-full', 'skidinc.battery.level', '>=', 10),
        new skidinc.achievements.create('电力III', '将你的电池升为等级25。', 'batteryLevel', 'fa-battery-full', 'skidinc.battery.level', '>=', 25),
        new skidinc.achievements.create('电力IV', '将你的电池升为等级50。', 'batteryLevel', 'fa-battery-full', 'skidinc.battery.level', '>=', 50),
        new skidinc.achievements.create('电力V', '将你的电池升为等级75。', 'batteryLevel', 'fa-battery-full', 'skidinc.battery.level', '>=', 75)
    ];
    
    for (var i = 0; i < skidinc.achievements.ach.length; i++) {
        var achievement = skidinc.achievements.ach[i];
        
        skidinc.achievements.owned.push(false);
        
        if (skidinc.achievements.categories.indexOf(achievement.category) == -1)
            skidinc.achievements.categories.push(achievement.category);
    };
    
    skidinc.loops.achievements = setInterval(function() {
        skidinc.achievements.loop();
    }, 1000);
};

skidinc.achievements.list = function() {
    var str = '',
        achievements = skidinc.achievements.getLast(),
        owned = skidinc.achievements.getOwnedAmount(),
        max = skidinc.achievements.ach.length;
    
    str += '<y>成就</y> ' + owned + '/' + max + ' 已完成:<br>';
    
    for (var i = 0; i < achievements.length; i++) {
        var ach = achievements[i];
        
        str += '<b>*</b> <z>' + ach.name + '</z>: ' + ach.desc + '<br>';
    };
    
    return skidinc.console.print(str);
};

skidinc.achievements.saveInit = function() {
    if (skidinc.achievements.ach.length !== skidinc.achievements.owned.length) {
        var diff = skidinc.achievements.ach.length - skidinc.achievements.owned.length;
        
        for (var i = 0; i < diff; i++)
            skidinc.achievements.owned.push(false);
    };
};

skidinc.achievements.update = function() {
    var achievements = skidinc.achievements.getLast(),
        owned = skidinc.achievements.getOwnedAmount(),
        max = skidinc.achievements.ach.length;
    
    $('#achievements-owned').html('已完成的成就 (' + owned + '/' + max + '):');
    
    for (var i = 0; i < achievements.length; i++) {
        var ach = achievements[i];
        
        // tether.io change title attr to data-original-title when loaded...
        $('#achievement-' + ach.category + '-tooltip').attr('data-original-title', ach.desc);
        $('#achievement-' + ach.category + '-name').html(ach.name);
    };
};

skidinc.achievements.domInit = function() {
    var achievements = skidinc.achievements.getLast(),
        owned = skidinc.achievements.getOwnedAmount(),
        max = skidinc.achievements.ach.length;
    
    $('#achievements-owned').html('成就已完成 (' + owned + '/' + max + '):');
    $('#achievements-content').append('<div id="achievements-row" class="row"></div>');
    
    for (var i = 0; i < achievements.length; i++) {
        $('#achievements-row').append('<div id="achievement-' + achievements[i].category + '" class="col-md-2 achievement-col">' +
            '<div id="achievement-' + achievements[i].category + '-tooltip" class="achievement-block" data-animation="false" data-toggle="tooltip" data-placement="top" title="' + achievements[i].desc + '">' +
                '<i class="fa ' + achievements[i].icon + '" aria-hidden="true"></i>' +
            '</div>' +
            '<p id="achievement-' + achievements[i].category + '-name">' + achievements[i].name + '</p>' +
        '</div>');
    };
};

skidinc.achievements.loop = function() {
    for (var i = 0; i < skidinc.achievements.ach.length; i++) {
        var achievement = skidinc.achievements.ach[i],
            owned = skidinc.achievements.owned[i];
        
        if (!owned) {
            var test = eval(achievement.varCheck + achievement.typeCheck + achievement.amount);
            
            if (test) {
                skidinc.achievements.owned[i] = true;
                skidinc.console.print('<y>成就</y>你获得了一个新的成就: <b>' + achievement.name + ', ' + achievement.desc.toLowerCase() + '</b>');
                
                skidinc.achievements.update();
            };
        };
    };
};